package com.freeter.modules.gjs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.freeter.modules.gjs.entity.UseRecord;
import com.freeter.modules.gjs.mapper.UseRecordMapper;
import com.freeter.modules.gjs.service.UseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class UseRecordServiceImpl implements UseRecordService {
    
    @Autowired
    private UseRecordMapper useRecordMapper;
    
    @Override
    @Transactional
    public boolean save(UseRecord useRecord) {
        if (useRecord.getRecordTime() == null) {
            useRecord.setRecordTime(new Date());
        }
        if (useRecord.getIsDeleted() == null) {
            useRecord.setIsDeleted(0);
        }
        if (useRecord.getCalcStatus() == null) {
            useRecord.setCalcStatus(0);
        }
        if (useRecord.getIsTransfer() == null) {
            useRecord.setIsTransfer(0);
        }
        return useRecordMapper.insert(useRecord) > 0;
    }
    
    @Override
    @Transactional
    public boolean update(UseRecord useRecord) {
        return useRecordMapper.updateById(useRecord) > 0;
    }
    
    @Override
    public UseRecord getById(Long id) {
        return useRecordMapper.selectById(id);
    }
    
    @Override
    public List<UseRecord> getByStockId(Long stockId) {
        return useRecordMapper.selectByStockId(stockId);
    }
    
    @Override
    public List<UseRecord> getByUserId(Long userId) {
        return useRecordMapper.selectByUserId(userId);
    }
    
    @Override
    public List<UseRecord> getByCalcStatus(Integer calcStatus) {
        return useRecordMapper.selectByCalcStatus(calcStatus);
    }
    
    @Override
    public List<UseRecord> getBySource(String source) {
        return useRecordMapper.selectBySource(source);
    }
    
    @Override
    public List<UseRecord> getByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        return useRecordMapper.selectByTimeRange(startTime, endTime);
    }
    
    @Override
    public List<UseRecord> getByStockIdAndTimeRange(Long stockId, LocalDateTime startTime, LocalDateTime endTime) {
        return useRecordMapper.selectByStockIdAndTimeRange(stockId, startTime, endTime);
    }
    
    @Override
    public List<UseRecord> getAllActive() {
        return useRecordMapper.selectAllActive();
    }
    
    @Override
    public UseRecord getByRefNo(String refNo) {
        return useRecordMapper.selectByRefNo(refNo);
    }
    
    @Override
    public List<UseRecord> getByPoNo(String poNo) {
        return useRecordMapper.selectByPoNo(poNo);
    }
    
    @Override
    public List<UseRecord> getByPrNo(String prNo) {
        return useRecordMapper.selectByPrNo(prNo);
    }
    
    @Override
    @Transactional
    public boolean updateCalcStatus(Long id, Integer calcStatus) {
        return useRecordMapper.updateCalcStatus(id, calcStatus) > 0;
    }
    
    @Override
    @Transactional
    public boolean softDelete(Long id, String updateRemarks) {
        return useRecordMapper.softDelete(id, updateRemarks) > 0;
    }
    
    @Override
    public Long countByStockId(Long stockId) {
        return useRecordMapper.countByStockId(stockId);
    }
    
    @Override
    public Long countByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        return useRecordMapper.countByTimeRange(startTime, endTime);
    }
    
    @Override
    @Transactional
    public boolean batchSave(List<UseRecord> useRecords) {
        if (useRecords == null || useRecords.isEmpty()) {
            return false;
        }
        
        for (UseRecord useRecord : useRecords) {
            if (useRecord.getRecordTime() == null) {
                useRecord.setRecordTime(new Date());
            }
            if (useRecord.getIsDeleted() == null) {
                useRecord.setIsDeleted(0);
            }
            if (useRecord.getCalcStatus() == null) {
                useRecord.setCalcStatus(0);
            }
            if (useRecord.getIsTransfer() == null) {
                useRecord.setIsTransfer(0);
            }
            useRecordMapper.insert(useRecord);
        }
        return true;
    }
    
    @Override
    public List<UseRecord> getByCondition(UseRecord condition, int page, int size) {
        EntityWrapper<UseRecord> wrapper = new EntityWrapper<>();
        
        if (condition != null) {
            if (condition.getStockId() != null) {
                wrapper.eq("stock_id", condition.getStockId());
            }
            if (condition.getUserId() != null) {
                wrapper.eq("user_id", condition.getUserId());
            }
            if (condition.getCalcStatus() != null) {
                wrapper.eq("calc_status", condition.getCalcStatus());
            }
            if (condition.getSource() != null && !condition.getSource().trim().isEmpty()) {
                wrapper.eq("source", condition.getSource());
            }
            if (condition.getRefNo() != null && !condition.getRefNo().trim().isEmpty()) {
                wrapper.like("ref_no", condition.getRefNo());
            }
            if (condition.getPoNo() != null && !condition.getPoNo().trim().isEmpty()) {
                wrapper.like("po_no", condition.getPoNo());
            }
            if (condition.getPrNo() != null && !condition.getPrNo().trim().isEmpty()) {
                wrapper.like("pr_no", condition.getPrNo());
            }
        }
        
        wrapper.eq("is_deleted", 0);
        wrapper.orderBy("record_time", false);
        
        Page<UseRecord> pageObj = new Page<>(page, size);
        return useRecordMapper.selectPage(pageObj, wrapper);
    }
} 